from publicHandle.publicHandle import returnData, conn, cur


# 全部课程信息查询
def couAllSearch():
    connection = conn()
    cursor = cur(connection)
    mysql_allStu = "select name,teacherName,credit,time,courseId from course"
    try:
        cursor.execute(mysql_allStu)
        data = cursor.fetchall()
        temp = {}
        result = []
        if data != None:
            for i in data:
                temp["name"] = i[0]
                temp["teacherName"] = i[1]
                temp["credit"] = i[2]
                temp["time"] = i[3]
                temp["courseId"] = i[4]
                result.append(temp.copy())
            return returnData(result)
        else:
            return returnData(result)
    except:
        return returnData({}, "访问数据库失败")
    finally:
        connection.commit()
        connection.close()
        cursor.close()


# 课程名查询
def couSearch(name=None):
    connection = conn()
    cursor = cur(connection)
    mysql_uniqueCourse = "select courseId,name,teacherName,credit,time from course where name = %s"
    try:
        cursor.execute(mysql_uniqueCourse, (name))
        data = cursor.fetchone()
        arr = []
        temp = {}
        temp["courseId"] = data[0]
        temp["name"] = data[1]
        temp["teacherName"] = data[2]
        temp["credit"] = data[3]
        temp["time"] = data[4]
        arr.append(temp.copy())
        return returnData(arr)
    except:
        return returnData()
    finally:
        connection.commit()
        connection.close()
        cursor.close()


# 增加课程
def couAdd(courseId, name, teacherName, credit, time):
    connection = conn()
    cursor = cur(connection)
    mysql_addStu = "insert into course(courseId,name,teacherName,credit,time) values(%s,%s,%s,%s,%s)"
    try:
        cursor.execute(mysql_addStu, (courseId, name, teacherName, credit, time))
        return returnData({"success": "true"})
    except:
        return returnData({"success": "false"}, "访问数据库错误")
    finally:
        connection.commit()
        connection.close()
        cursor.close()


# 删除课程
def couDelete(courseId):
    connection = conn()
    cursor = cur(connection)
    mysql_uniqueCourse = "delete from course where courseId = %s"
    try:
        cursor.execute(mysql_uniqueCourse, (courseId))
        return returnData({"success": "true"})
    except:
        return returnData({"success": "false"})
    finally:
        connection.commit()
        connection.close()
        cursor.close()


# 修改课程信息
def couEdit(courseId, name, teacherName, credit, time):
    connection = conn()
    cursor = cur(connection)
    mysql_searchCourse = "select courseId from course where courseId=%s"
    try:
        cursor.execute(mysql_searchCourse, (courseId))
        data = cursor.fetchone()
        if (data == None):
            return returnData({"success": "false"}, "课程不存在")
        else:
            pass
    except:
        return returnData({"success": "false"}, "访问数据库失败")
    mysql_editCourse = "update course set name = %s,teacherName=%s,credit=%s,time=%s where courseId=%s"
    try:
        cursor.execute(mysql_editCourse, (name, teacherName, credit, time, courseId))
        return returnData({"success": "true"})
    except:
        return returnData({"success": "false"}, "访问数据库失败")
    finally:
        connection.commit()
        connection.close()
        cursor.close()
